/*
@title: Taco Man's Taco Stand
@author: David, Etah, Oliver, and Collin
@tags: []
@addedOn: 01-10-2025
@description: Run your own taco stand by preparing tacos according to recipes. Collect ingredients like tortillas, meat, tomatoes, onions, sauce, and cheese. Cook the meat on the frying pan and assemble tacos in the correct order to score points.
*/
let randomDecimal = Math.random();

// Sprite Initializations (the same as before)
const player = "P";
const tomatoes = "t";
const onions = "o";
const sauce = "e";
const meat = "m";
const cooked_meat = "z";
const frying_pan = "f";
const cheese = "c";
const checkmark = "x";
const trash = "n";
const casing = "q";
const shell = "s";
const plate = "p";

// Variables for game state
let meat_condition = false;
let cooked = false;
let cookedMeatCount = 0; // Track number of cooked meats
let touching_object = null;
let player_coords = [0, 0];
let playerX;
let playerY;
let recipe = [];
let recipe_answer = getRandomNumberInBase(0, 1296, 5);
let recipe_decoded = ['tortilla', 'meat'];
let tortilla_obtained = false;
let tomatoes_obtained = false;
let onions_obtained = false;
let sauce_obtained = false;
let obtained_meat = false;
let cheese_obtained = false;
let verify_recipe = 0;
let points = 0;
let global_priority = 0;

// Coordinates for interactable objects
let interactableObjects = [
  { name: "tomatoes", coords: [1, 6] },
  { name: "onions", coords: [2, 6] },
  { name: "sauce", coords: [3, 6] },
  { name: "cheese", coords: [4, 6] },
  { name: "meat", coords: [7, 6] },
  { name: "pan3", coords: [7, 4] },
  { name: "plate4", coords: [4, 4] },
  { name: "tortilla", coords: [5, 6] },
  { name: "checkmark", coords: [4, 2] }
];

//Set legand 
setLegend(
  [player, bitmap`
DDDDDDDDDDDDDDDD
D44444444444444D
D4.....FF.....4D
D4....FFFF....4D
D4..FFFFFFFF..4D
D4....4D4D....4D
D4....0DD0....4D
D4...CDCCDC...4D
D4DDD.CD4C.D4D4D
D4D4D4DDDDD4DD4D
D4..4D4DDD4D..4D
D4....DDD4....4D
D4....4D4D....4D
D4...DDDDDD...4D
D44444444444444D
DDDDDDDDDDDDDDDD`],
  [tomatoes, bitmap`
................
.........4......
.......44.......
.......343......
......33383.....
......33333.....
......33333.....
.......333......
................
................
................
................
................
................
................
................` ],
  [onions, bitmap`
................
................
......3.........
......83........
.....3883.......
....388883......
....383383......
....388883......
.....3333.......
................
................
................
................
................
................
................` ],
  [sauce, bitmap`
................
.......3........
.......6........
......666.......
......333.......
......333.......
......6D6.......
......636.......
......333.......
................
................
................
................
................
................
................` ],
  [meat, bitmap`
................
................
................
......33........
.....333........
.....3333.......
....333333......
................
................
................
................
................
................
................
................
................`],
  [cooked_meat, bitmap`
................
................
................
......CC........
.....CFC........
.....CFCC.......
....CCCFCC......
................
................
................
................
................
................
................
................
................`],
  [frying_pan, bitmap`
................
................
................
................
................
................
................
...00011111.....
......LLLLL.....
......39393.....
.......696......
................
................
................
................
................`],
  [checkmark, bitmap`
................
................
............DD..
...........D4D..
...........D4D..
...........D4D..
..DDD.....DD4D..
..D4DD....D4DD..
..DD4DD..DD4D...
...D44DDDD44D...
...DD44DD44DD...
....DD4444DD....
.....DD44DD.....
......DDDD......
................
................`],
  [trash, bitmap`
................
................
................
................
..111......111..
..111LLLLLL111..
...11LLLLLL11...
...11LLLLLL11...
...11LLLLLL11...
....1LLLLLL1....
....11LLLL11....
.....1LLLL1.....
.....111111.....
................
................
................`],
  [cheese, bitmap`
................
................
................
......99.9......
.....666666.....
.....969.966....
....6696669.6...
....696.9966.6..
............6...
................
................
................
................
................
................
................`],
  [shell, bitmap`
................
................
................
................
..6..66..66..6..
..F..F6..6F..6..
..F..F6..6F..6..
..6..66..FF..F..
..6..66..66.66..
..F..66..66.6...
..F.6F6.FF666...
..666.6FF.......
................
................
................
................` ],
  [casing, bitmap`
................
.....6666F......
....6666666.....
...66F666666....
...66666F666....
...F666666F6....
...666666666....
...66F66F666....
....6666666.....
.....6666F......
................
................
................
................
................
................` ],
  [plate, bitmap`
................
................
................
................
................
................
................
................
.....22222......
....2211122.....
..2211LLL1122...
...221111122....
....2222222.....
................
................
................`]
)


// Define the letter mapping
const digitToLetter = {
  0: 'meat',
  1: 'tomatoes',
  2: 'onions',
  3: 'cheese',
  4: 'sauce',
};

generate_recipe();

let level = 0
const levels = [
  map`
.........
.........
....x....
.........
..P.p..f.
.........
.toecq.m.`
]

setMap(levels[level])


// Player movement controls
onInput("a", () => {
  getFirst(player).x -= 1
  player_coords[0] = getFirst(player).x
})

onInput("d", () => {
  getFirst(player).x += 1
  player_coords[0] = getFirst(player).x
})

onInput("w", () => {
  getFirst(player).y -= 1
  player_coords[1] = getFirst(player).y
})

onInput("s", () => {
  getFirst(player).y += 1
  player_coords[1] = getFirst(player).y
})

// Interaction on pressing "i"
onInput("i", () => {
  playerX = getFirst(player).x;
  playerY = getFirst(player).y;

  for (let obj of interactableObjects) {
    if (player_coords[0] === obj.coords[0] && player_coords[1] === obj.coords[1]) {
      touching_object = obj.name
    }
  }

  // Set meat condition to true if touching meat
  if (touching_object == "meat") {
    obtained_meat = true
  }


  if (touching_object == "onions" && cheese_obtained == false && tomatoes_obtained == false && sauce_obtained == false && tortilla_obtained == false) {
    onions_obtained = true;
  }

  if (touching_object == "cheese" && onions_obtained == false && tomatoes_obtained == false && sauce_obtained == false && tortilla_obtained == false) {
    cheese_obtained = true;
  }

  if (touching_object == "tomatoes" && cheese_obtained == false && onions_obtained == false && sauce_obtained == false && tortilla_obtained == false) {
    tomatoes_obtained = true;
  }

  if (touching_object == "sauce" && cheese_obtained == false && tomatoes_obtained == false && onions_obtained == false && tortilla_obtained == false) {
    sauce_obtained = true;
  }

  if (touching_object == "tortilla" && cheese_obtained == false && tomatoes_obtained == false && sauce_obtained == false && onions_obtained == false) {
    tortilla_obtained = true;
  }

  // Place meat on pan if over frying pan
  if (obtained_meat && (touching_object === "pan1" || touching_object === "pan2" || touching_object === "pan3" || touching_object === "pan4")) {
    obtained_meat = false
    addSprite(getFirst(player).x, getFirst(player).y, meat) // Meat appears on pan immediately
    let meat_1 = new meat_class(getFirst(player).x, getFirst(player).y) // Start cooking the meat
  }
  //Add ingrediants to plate
  if ((playerX == 4 && playerY == 4) && cooked == false) {
    //Check if tomatoes
    if (tomatoes_obtained == true) {
      addSprite(4, 4, tomatoes)
      recipe.push("tomatoes")
      tomatoes_obtained = false;
    }
    if (onions_obtained == true) {
      addSprite(4, 4, onions)
      recipe.push("onions")
      onions_obtained = false;
    }
    if (sauce_obtained == true) {
      addSprite(4, 4, sauce)
      recipe.push("sauce")
      sauce_obtained = false;
    }
    if (cheese_obtained == true) {
      addSprite(4, 4, cheese)
      recipe.push("cheese")
      cheese_obtained = false;
    }
    if (tortilla_obtained == true) {
      addSprite(4, 4, casing)
      recipe.push("tortilla")
      tortilla_obtained = false;
    }
  } else if (cooked) { //Cooked meat code
    // Check for cooked meat and add to plate
    if (cooked && (touching_object === "plate1" || touching_object === "plate2" || touching_object === "plate3" || touching_object === "plate4")) {
      if (cookedMeatCount >= 1) {
        playerX = getFirst(player).x
        playerY = getFirst(player).y
        // Add cooked meat to the plate
        addSprite(getFirst(player).x, getFirst(player).y, cooked_meat)
        cookedMeatCount--;
        clearTile(7, 4)
        addSprite(7, 4, frying_pan)
        cooked = false
        recipe.push("meat")
      }
    }
  }

  if (playerX == 4, playerY == 2) {
    for (i = 0; i <= countDigits(recipe_answer) + 1; i++) {
      if (recipe[i] == recipe_decoded[i]) {
        verify_recipe++;
      } else {
        verify_recipe = 0;
        recipe.length = 0;
        recipe_answer = getRandomNumberInBase(0, 1296, 5);
        recipe_decoded.length = 2;
        generate_recipe();
        // Clear Plate
        clearTile(4, 4)
        addSprite(4, 4, plate)
        //regenerate text
        clearText()
        for (i = 0; i <= countDigits(recipe_answer) + 1; i++) {
          addText("" + recipe_decoded[i], {
            x: 12,
            y: i + 1,
          })
        }
        //regenerate point text
        addText("Points: " + points, {
          x: 0,
          y: 1
        })
        break
      }
    }
    //recipe verified correctly
    if (verify_recipe == countDigits(recipe_answer) + 2) {
      points++;
      verify_recipe = 0;
      recipe.length = 0;
      recipe_answer = getRandomNumberInBase(0, 1296, 5);
      recipe_decoded.length = 2;
      generate_recipe();
      //Clear Plate
      clearTile(4, 4)
      addSprite(4, 4, plate)
      //regenerate text
      for (i = 0; i <= countDigits(recipe_answer) + 1; i++) {
        addText("" + recipe_decoded[i], {
          x: 12,
          y: i + 1,
        })
      }
      // Update Points
      addText("Points: " + points, {
        x: 0,
        y: 1
      })
    }
  }
})

// Meat class for cooking logic
class meat_class {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.countdown = 1;
    this.cookCountdown();
  }

  // Countdown for cooking meat
  cookCountdown() {
    if (this.countdown <= 0) {
      playerX = getFirst(player).x
      playerY = getFirst(player).y
      cooked = true; // Update cooked to true when cooked
      clearTile(this.x, this.y); // Remove uncooked meat
      addSprite(this.x, this.y, cooked_meat); // Place cooked meat
      addSprite(this.x, this.y, frying_pan);
      if (this.x == playerX && this.y == playerY) {
        addSprite(this.x, this.y, player);
      }
      cookedMeatCount += 1;
    } else {
      setTimeout(() => {
        this.countdown--;
        this.cookCountdown();
      }, 1000);
    }
  }
}

function getRandomNumberInBase(min, max, base) {
  let randomNumber = getRandomInt(min, max);
  return randomNumber.toString(base);
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

function generate_recipe() {
  recipe_decoded = ["tortilla", "meat"]
  for (let i = 0; i < recipe_answer.length; i++) {
    let digit = recipe_answer[i]; // Get each digit as a string
    recipe_decoded.splice(recipe_decoded.length, 0, digitToLetter[digit]); // Map to a letter and add to the list
  }
}

function countDigits(num) {
  // Ensure the number is positive (optional)
  num = Math.abs(num);

  // Convert the number to a string and return its length
  return num.toString().length;
}

clearText()
for (i = 0; i <= countDigits(recipe_answer) + 1; i++) {
  addText("" + recipe_decoded[i], {
    x: 12,
    y: i + 1,
  })
}

addText("Points: " + points, {
  x: 0,
  y: 1
})
